package com.zoweunion.mechanic.service.base.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.dao.base.DailyDataDao;
import com.zoweunion.mechanic.service.base.BaseService;
import com.zoweunion.mechanic.service.base.DailyDataService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.Orgin;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.zoweunion.mechanic.service.app.impl.OrderServicelmpl.createYMD;

@Service
@Transactional
@SuppressWarnings("unchecked")
public class DailyDataServiceImpl extends BaseService implements DailyDataService {

    @Autowired
    private DailyDataDao dailyDataDao;

    /**
     * 日报数据（ktbl）
     * type:month日报数据，year月报数据
     * date:月、日格式都为yyyy-mm-dd，
     */
    @Override
    public PageInfo dailyData(Map<String, Object> reqMap) throws MyException {
        Integer pageSize = Integer.valueOf(reqMap.get("pageSize").toString());
        Integer currentPage = Integer.valueOf(reqMap.get("currentPage").toString());
        PageHelper.startPage(currentPage, pageSize);
        List<Map<String, Object>> dailyData = new ArrayList<>();
//        if (Orgin.getString(reqMap, "sId").equals("6bd970ddd3e44e0f8646f951453ce18e")) {
            if (Orgin.getString(reqMap, "type").equals("day")) {
                Date dateS = Orgin.getDate(reqMap, "dateS");
                Date dateE = Orgin.getDate(reqMap, "dateE");
                List<String> ymd = createYMD(dateS, dateE);
                if (ymd.size()>30) {
                    throw new MyException(Constants.CODE_HANDEL_FAILED, "所选时间超过30天");
                }
                reqMap.put("dateS", Orgin.getString(reqMap, "dateS") + " 00:00:00");
                reqMap.put("dateE", Orgin.getString(reqMap, "dateE") + " 23:59:59");
                dailyData = dailyDataDao.dailyData(reqMap);
            } else {
                dailyData = dailyDataDao.monthlyReportData(reqMap);
            }
//        }
        PageInfo<Map<String, Object>> mapPageInfo = new PageInfo<>(dailyData);
        return mapPageInfo;
    }
}
